package org.hawk.gwt.ppc.utils;

import java.util.Arrays;

import junit.framework.TestCase;

public class StringUtilsTestCase extends TestCase {
	public void testReplaceAll(){
		assertEquals("", StringUtils.replaceAll("", "", ""));
		try{
			StringUtils.replaceAll("", "", null);
			fail("NullPointerException expected");
		}
		catch(NullPointerException ex){}
		assertEquals("", StringUtils.replaceAll("", "aaa", "s"));
		assertNull(StringUtils.replaceAll(null, "aaa", "s"));
		assertEquals("aaa", StringUtils.replaceAll("aaa", null, "s"));
		assertEquals("aaa", StringUtils.replaceAll("aaa", "", "s"));
		assertEquals("aba", StringUtils.replaceAll("aca", "c", "b"));
		assertEquals("Alexander", StringUtils.replaceAll("Alexey", "ey", "ander"));
		assertEquals("I like beer in the morning", 
				StringUtils.replaceAll("I like milk in the morning", "milk", "beer"));
		assertEquals("Alex loves Dasha and Lena :)", 
				StringUtils.replaceAll("Alex loves Lena :)", "Lena", "Dasha and Lena"));
	}
	
	public void testSplit(){
		assertTrue(Arrays.equals(new String[]{"aaa","bbb"}, 
			StringUtils.split("aaa.bbb",".")));
		assertTrue(Arrays.equals(new String[]{"aaa","bbb"}, 
				StringUtils.split("aaaccbbb","cc")));
		assertTrue(Arrays.equals(new String[]{"aaa","","",""}, 
				StringUtils.split("aaabbb","b")));
		assertTrue(Arrays.equals(new String[]{"a","b","c"}, 
				StringUtils.split("abc","")));
		try{
			StringUtils.split("abc",null);
			fail();
		}
		catch(NullPointerException ex){}
		assertTrue(Arrays.equals(new String[]{"abc"}, 
				StringUtils.split("abc","d")));
		assertTrue(Arrays.equals(new String[]{"","bc"}, 
				StringUtils.split("abc","a")));
		assertTrue(Arrays.equals(new String[]{"Never "," up"}, 
				StringUtils.split("Never give up","give")));
		assertTrue(Arrays.equals(new String[]{"Never","give","up"}, 
				StringUtils.split("Never give up"," ")));
		assertTrue(Arrays.equals(new String[]{"Alex"}, 
				StringUtils.split("Alex","Alexander")));
		assertTrue(Arrays.equals(new String[]{"",""}, 
				StringUtils.split("Alex","Alex")));
	}
	
	
	public void testReplaceLast() {
		assertEquals("", StringUtils.replaceLast("", "aaaa", "ss"));
		assertNull(StringUtils.replaceLast(null, "", ""));
		assertEquals("aaa", StringUtils.replaceLast("aaa", null, "sss"));
		assertEquals("sss", StringUtils.replaceLast("sss", "", "er"));
		assertEquals("ss", StringUtils.replaceLast("sss", "s", null));
		assertEquals("ss", StringUtils.replaceLast("sss", "s", ""));
		assertEquals("abcrr", StringUtils.replaceLast("abca", "a", "rr"));
		assertEquals("acbcdb", StringUtils.replaceLast("acbcacb", "ac", "d"));
		assertEquals("abcdabcdabcd!!!ddd", StringUtils.replaceLast("abcdabcdabcdabcddd", "abc", "!!!"));
		assertEquals("abcdabcdabcdddd", StringUtils.replaceLast("abcdabcdabcdabcddd", "abc", ""));
		assertEquals("rrr", StringUtils.replaceLast("sss", "sss", "rrr"));
	}
}
